package com.hyh.admin.sys.controller.upload;

import com.hyh.ad.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/upload")
@Api(tags = "上传文件")
public class UploadController {

    @Value("${ad.profile}")
    private String UPLOAD_DIR;

    @Value("${upload.avatar}")
    private String AVATAR_DIR;

    /**
     * 上传文件
     * @param files
     * @return 文件名列表
     */
    @PostMapping("/file")
    public AjaxResult uploadFiles(@RequestParam("files") MultipartFile[] files) {
        Map<String, String> map = new HashMap<>();

        // 获取项目的绝对路径
        String projectPath = System.getProperty("user.dir");
        Path uploadPath = Paths.get(projectPath, UPLOAD_DIR);

        // 确保上传目录存在
        if (!Files.exists(uploadPath)) {
            try {
                Files.createDirectories(uploadPath);
            } catch (IOException e) {
                map.put("msg", "创建上传目录失败：" + e.getMessage());
                return AjaxResult.error("操作失败",map);
            }
        }

        for (MultipartFile file : files) {
            if (file.isEmpty()) {
                map.put("msg", "文件为空，上传失败");
                continue;
            }

            try {
                String originalFilename = file.getOriginalFilename();
                String timestampedFilename = System.currentTimeMillis() + "_" + originalFilename;

                // 保存文件
                File destinationFile = new File(uploadPath.toFile(), timestampedFilename);
                file.transferTo(destinationFile);

                map.put("msg", "文件上传成功");
                map.put("filename", timestampedFilename);
                //存放访问路径
                map.put("url", "/uploads/" + timestampedFilename);
            } catch (IOException e) {
                e.printStackTrace();
                map.put("msg", "文件上传失败：" + e.getMessage());
            }
        }

        return AjaxResult.success("操作成功", map);
    }

    /**
     * 上传头像
     * @param file
     * @return 图片路径
     */
    @PostMapping("/avatar")
    public AjaxResult uploadAvatar(@RequestParam("file") MultipartFile file) {
        Map<String, String> map = new HashMap<>();

        // 获取项目的绝对路径
        String projectPath = System.getProperty("user.dir");
        Path uploadPath = Paths.get(projectPath, AVATAR_DIR);

        // 确保上传目录存在
        if (!Files.exists(uploadPath)) {
            try {
                Files.createDirectories(uploadPath);
            } catch (IOException e) {
                map.put("msg", "创建上传目录失败：" + e.getMessage());
                return AjaxResult.error("操作失败",map);
            }
        }

        if (file.isEmpty()) {
            map.put("msg", "文件为空，上传失败");
            return AjaxResult.error("操作失败",map);
        }

        try {
            String originalFilename = file.getOriginalFilename();
            String timestampedFilename = System.currentTimeMillis() + "_" + originalFilename;

            // 保存文件
            File destinationFile = new File(uploadPath.toFile(), timestampedFilename);
            file.transferTo(destinationFile);

            map.put("msg", "文件上传成功");
            map.put("filename", timestampedFilename);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("msg", "文件上传失败：" + e.getMessage());
        }

        return AjaxResult.success("操作成功", map);
    }
}
